package main.od;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public class AliFindGold {
    public static void main(String[] args) {
    // 把环拉直
        Scanner scanner = new Scanner(System.in);
        int[] inputArr = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        Stack<Integer> stack = new Stack<>();
        int n = inputArr.length;
        int[] resultArr = new int[n];
        Arrays.fill(resultArr,-1);
        for (int i = 0;i < 2 * n - 1;i++){
            while (!stack.isEmpty() && inputArr[stack.peek()] < inputArr[i % n]){
                resultArr[stack.pop()] = inputArr[i % n];
            }
            stack.push(i%n);
        }
        StringBuilder outputStr = new StringBuilder();
        for (int i = 0;i < n;i++){
            outputStr.append(resultArr[i]).append(",");
        }
        System.out.println("result: " + outputStr.substring(0,outputStr.length()-1));
    }
}
